<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
	String roleid = request.getParameter("roleid");
	if (roleid == null) {
		roleid = "";
	}
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <jsp:include page="../inc/meta.jsp"></jsp:include>
	<jsp:include page="../inc/easyui.jsp"></jsp:include>
	
	<script type="text/javascript">
		$(function(){
			$("#tree").tree({
				url : "<%=request.getContextPath()%>/menu/menuList",
				checkbox : true,
				cascadeCheck : true,
// 				lines:true,
				parentField : 'pid',
				onClick: function(node){
					if(node.checked)
					{
						$("#tree").tree('uncheck', node.target);
					}else
					{
						$("#tree").tree('check', node.target);
					}
				},
				onLoadSuccess:function(node,data){
					$.ajax({
						url : "grantbyrole",
						cache : false,
						dataType : "json",
						data : "roleid="+$("#role_id").val(),
						success : function(data) {
							if (data.success) {
								if(data.list!="" && data.list!=undefined){
									for ( var i = 0; i < data.list.length; i++) {
 										//查找一个节点然后返回它
										var node = $("#tree").tree("find", data.list[i].menu_id);
										if (node) {
											$("#tree").tree('check', node.target);
										}
										
									}
								}
							}
						}
					});
					$("#tree").tree('expandAll');
				}
			});
		});
		
		function grant($dialog,roleid,$parent){
			var node = $("#tree").tree("getChecked");
					var modules = "";
					if (node.length > 0) {
						for ( var i = 0; i < node.length; i++) {
							if(modules==""){
								modules = node[i].id;
							}else{
								modules += ","+node[i].id;
							}
						}
					}
					parent.$.messager.progress({
						msg : '数据处理中。。。'
					});
					$.ajax({
						url : "grant",
						data : "roleid="+roleid+"&modules="+modules,
						cache : false,
						dataType : "json",
						success : function(data) {
							parent.$.messager.progress('close');
							if (data.success) {
								$dialog.dialog('destroy');
								$parent.messager.alert('提示', data.msg, 'info');
							}else
							{
								$parent.messager.alert('提示', data.msg, 'info');
							}
						}
					});
		}
		
		//展开树形节点
	function expandAll() {
		var node = $("#tree").tree('getSelected');
		if (node) {
			$("#tree").tree('expandAll', node.target);
		} else {
			$("#tree").tree('expandAll');
		}
	}
	//折叠树形节点
	function collapseAll() {
		var node = $("#tree").tree('getSelected');
		if (node) {
			$("#tree").tree('collapseAll', node.target);
		} else {
			$("#tree").tree('collapseAll');
		}
	}
	
	//全选
	function checkAll() {
		var nodes = $("#tree").tree('getChecked', 'unchecked');
		if (nodes && nodes.length > 0) {
			for ( var i = 0; i < nodes.length; i++) {
				$("#tree").tree('check', nodes[i].target);
			}
		}
	}
	//取消
	function uncheckAll() {
		var nodes = $("#tree").tree('getChecked');
		if (nodes && nodes.length > 0) {
			for ( var i = 0; i < nodes.length; i++) {
				$("#tree").tree('uncheck', nodes[i].target);
			}
		}
	}
	//反选
	function checkInverse() {
		var unchecknodes = $("#tree").tree('getChecked', 'unchecked');
		var checknodes = $("#tree").tree('getChecked');
		if (unchecknodes && unchecknodes.length > 0) {
			for ( var i = 0; i < unchecknodes.length; i++) {
				$("#tree").tree('check', unchecknodes[i].target);
			}
		}
		if (checknodes && checknodes.length > 0) {
			for ( var i = 0; i < checknodes.length; i++) {
				$("#tree").tree('uncheck', checknodes[i].target);
			}
		}
	}
	</script>
  </head>
  
  <body>
  	<input type="hidden" id="role_id" value="<%=roleid%>"/>
    	<div class="easyui-layout" data-options="fit:true">
			<div data-options="region:'west'" title="角色可以访问的权限"
				style="width:300px;padding:10px">
				<div id="tree"></div>
			</div>
			<div data-options="region:'center'" style="padding:10px">
				<a id="btn" class="easyui-linkbutton" onclick="expandAll();" data-options="iconCls:'icon-redo'">展开</a>
				<br> <br>
				<a id="btn" class="easyui-linkbutton" onclick="collapseAll();"data-options="iconCls:'icon-undo'">折叠</a>
				<br> <br>
				<a id="btn" class="easyui-linkbutton" onclick="checkAll();"data-options="iconCls:'icon-ok'">全选</a>
				<br> <br>
				<a id="btn" class="easyui-linkbutton" onclick="checkInverse();"data-options="iconCls:'icon-undo'">反选</a>
				<br> <br>
				<a id="btn" class="easyui-linkbutton" onclick="uncheckAll();"data-options="iconCls:'icon-no'">取消</a>
			</div>
		</div>
  </body>
</html>
